package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.pojo.Comment;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@PreAuthorize("@permission.admin()")
@RestController
@RequestMapping("/admin/comment")
public class CommentAdminApi {

    @Autowired
    ICommentService commentService;

    /**
     * 添加评论
     *
     * @param _comment 评论对象
     * @return 自定义返回对象
     */
    @ApiOperation("添加评论")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.input_data_not_complete_code, message = ResponseState.input_data_not_complete_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PostMapping
    public ResponseResult insert(@RequestBody Comment _comment) {
        return commentService.insert(_comment);
    }

    /**
     * 删除评论
     *
     * @param _Id 评论id
     * @return 自定义返回对象
     */
    @ApiOperation("删除评论")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.input_data_not_complete_code, message = ResponseState.input_data_not_complete_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") String _Id) {
        return commentService.delete(_Id);
    }

    /**
     * 查询评论
     *
     * @param _Id 评论id
     * @return 自定义返回对象
     */
    @ApiOperation("查询评论")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/{id}")
    public ResponseResult query(@PathVariable("id") String _Id) {
        return commentService.query(_Id);
    }

    /**
     * 置顶评论
     *
     * @param _Id 评论id
     * @return 自定义返回对象
     */
    @ApiOperation("置顶评论")
    @ApiResponses({
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PutMapping("/top/{id}")
    public ResponseResult topCommit(@PathVariable("id") String _Id) {
        return commentService.topCommit(_Id);
    }

    /**
     * 获取文章评论列表
     *
     * @return 自定义返回对象
     */
    @ApiOperation("获取文章评论列表")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message)
    })
    @GetMapping("/list/{page}/{size}")
    public ResponseResult list(@PathVariable int size, @PathVariable int page) {
        return commentService.listForAdmin(page, size);
    }


    /**
     * 获取评论数量
     *
     * @return 自定义返回对象
     */
    @ApiOperation("获取评论数量")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/count")
    public ResponseResult getCommentCount() {
        return commentService.getCommentCount();
    }
}
